
import Head from 'next/head'

<Head>
  <script>
    {
      `(function() {
         var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?e60fb290e204e04c5cb6f79b0ac1e697";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
       })();`
    }
  </script>
</Head>

![LangChain](https://pica.zhimg.com/50/v2-56e8bbb52aa271012541c1fe1ceb11a2_r.gif)





DeepInfra[#](#deepinfra "Permalink to this headline")
=====================================================

`DeepInfra` 提供了多种LLM [several LLMs](https://deepinfra.com/models).

本文介绍如何使用Langchain与[DeepInfra](https://deepinfra.com)进行交互。 

Imports[#](#imports "Permalink to this headline")
-------------------------------------------------

```python
import os
from langchain.llms import DeepInfra
from langchain import PromptTemplate, LLMChain

```

设置环境变量的API Key[#](#set-the-environment-api-key "Permalink to this headline")
-----------------------------------------------------------------------------------------

请确保从DeepInfra获取API Key。您必须[登录](https://deepinfra.com/login?from=%2Fdash)并获取新令牌。

您将获得1个小时的免费服务器级GPU计算时间，以测试不同的模型（请参见[此处](https://github.com/deepinfra/deepctl#deepctl))。

您可以使用 `deepctl auth token` 命令打印您的令牌。

```python
# get a new token: https://deepinfra.com/login?from=%2Fdash

from getpass import getpass

DEEPINFRA_API_TOKEN = getpass()

```

```python
os.environ["DEEPINFRA_API_TOKEN"] = DEEPINFRA_API_TOKEN

```

创建DeepInfra实例[#](#create-the-deepinfra-instance "本标题的永久链接")
-----------------------------------------------------------

确保先通过`deepctl deploy create -m google/flat-t5-xl`部署模型（参见[此处](https://github.com/deepinfra/deepctl#deepctl))

```python
llm = DeepInfra(model_id="DEPLOYED MODEL ID")

```

创建提示模板[#](#create-a-prompt-template "本标题的永久链接")
-----------------------------------------------

我们将为问题和答案创建提示模板。

```python
template = """Question: {question}

Answer: Let's think step by step."""

prompt = PromptTemplate(template=template, input_variables=["question"])

```

启动LLMChain[#](#initiate-the-llmchain "本标题的永久链接")
------------------------------------------------

```python
llm_chain = LLMChain(prompt=prompt, llm=llm)

```

运行LLMChain[#](#run-the-llmchain "本标题的永久链接")
-------------------------------------------

提供一个问题并运行LLMChain。

```python
question = "What NFL team won the Super Bowl in 2015?"

llm_chain.run(question)

```

